package com.demo.api.controller.api.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.api.controller.BaseController;
import com.demo.client.model.user.SysMenu;
import com.demo.api.service.user.IUserService;
import com.demo.api.service.user.IUserSystemService;
import com.demo.api.vo.user.MenuVo;
import com.demo.api.vo.user.RoleVo;
import com.demo.api.vo.user.UserVo;
import com.demo.core.annotation.AuthToken;
import com.demo.core.response.ApiResponse;
import com.demo.core.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Tan-Bowen
 * @version 1.0
 * @description TODO
 * @date 2021/7/27 14:52
 */

@Api(value = "用户信息相关接口", tags = "用户中心" )
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

    @Resource
    private IUserSystemService userSystemService;

    @Resource
    private IUserService userService;

    @AuthToken(checkPower = false)
    @PostMapping("/all")
    @ApiOperation(value = "用户列表查询")
    public ApiResponse<IPage<UserVo.UserRes>> userAll(PageUtil.Pages page,UserVo.Select select){
        return ApiResponse.success(userSystemService.userAll(getPage(page),select));
    }


    @AuthToken(checkPower = false)
    @PostMapping("/create")
    @ApiOperation(value = "用户新增")
    public ApiResponse<Void> create(@Validated UserVo.Create request,
                                    @RequestHeader(value = "userId",required = false)Long userId){
        userSystemService.create(request);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @PostMapping("/update")
    @ApiOperation(value = "用户编辑")
    public ApiResponse<Void> update(@Validated UserVo.Update update,
                                    @RequestHeader(value = "userId",required = false)Long userId){
        userSystemService.update(update);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @PostMapping("/userMenu")
    @ApiOperation(value = "获取用户菜单")
    public ApiResponse<List<SysMenu>> userMenu(@RequestHeader(value = "userId",required = false)Long userId){
        return ApiResponse.success(userService.getUserMenu(userId));
    }

    @AuthToken(checkPower = false)
    @GetMapping("/getMenuAndAuthId")
    @ApiOperation(value = "获取当前登录用户菜单和权限id")
    public ApiResponse<MenuVo.MenuAuthId> getMenuAndAuthId(){
        return ApiResponse.success(userService.getMenuAndAuthId());
    }


    @AuthToken(checkPower = false)
    @GetMapping("/getUserRole")
    @ApiOperation(value = "获取用户绑定角色信息") // 已改为 ：：一个用户用户只能拥有一个角色
    public ApiResponse<RoleVo.PageList> getMenuAndAuthId(@RequestParam("userId")Long userId){
        return ApiResponse.success(userService.getUserRole(userId));
    }

    @AuthToken
    @PostMapping("/bandUserRole")
    @ApiOperation(value = "绑定用户角色") // 已改为 ：：一个用户用户只能拥有一个角色
    public ApiResponse<Void> bandUserRole(@RequestParam("userId")Long userId,@RequestParam(value = "roleId",required = false)Long roleId){
        userService.bandUserRole(userId,roleId);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @PostMapping("/deleteUser")
    @ApiOperation("删除用户")
    public ApiResponse<Void> deleteUser(@RequestParam("userId")Long userId){
        userSystemService.deleteUser(userId);
        return ApiResponse.success();
    }

    @AuthToken(checkPower = false)
    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    public ApiResponse<Void> updatePassword(@RequestParam("newPass")String newPass,@RequestParam("oldPass")String oldPass){
        userSystemService.updatePassword(newPass,oldPass);
        return ApiResponse.success();
    }
}